/**
 * 
 */
package com.hoopchina.settings;

import java.io.File;
import java.io.IOException;

import com.hoopchina.news.R;
import com.hoopchina.util.xml.XMLDataNetwork;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Handler;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

/**
 * 设置：清除缓存设置
 * @author Guo Baiyu
 * 
 */
public class ClearCacheDialog extends DialogPreference {

	private Context	     mContext	  = null;
	private final String FILE_PATH = "/sdcard/baidunews/newstempfile/";

    private final int MSG_DeleteFailed = 2;
    private final int MSG_DeleteComplete = 1;
    private ProgressDialog _progressDialog;

	/**
	 * @param context
	 * @param attrs
	 */
	public ClearCacheDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public ClearCacheDialog(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private void init(Context context) {
		mContext = context;
	}

	@Override
	protected View onCreateDialogView() {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View layout = inflater.inflate(R.layout.dialogclearcache, null);
		if (null == layout)
			return super.onCreateDialogView();
		
		return layout;
	}
	public void onClick(DialogInterface dialog, int which) {
		// 清除缓存
		if (which == DialogInterface.BUTTON1) {
			_progressDialog = new ProgressDialog (mContext);
			_progressDialog.setTitle("");
			_progressDialog.setMessage("正在清空缓存");
			_progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  	
			_progressDialog.show();
			new Thread() {
				public void run() {

					
					File CacheFiles = new File(FILE_PATH); //strPath为路径
					   String[] fl = CacheFiles.list(); 
					   for(int i=0; i < fl.length; i++) { 
					    File fcur = new File(CacheFiles.getAbsolutePath() + "/" + fl[i]); 
					     	fcur.delete();
					    }
					   mHandler.sendEmptyMessage(MSG_DeleteComplete);
				}
			}.start();


		}
		super.onClick(dialog, which);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_DeleteFailed:
				break;
			case MSG_DeleteComplete:
				   _progressDialog.dismiss();
				   Toast.makeText(mContext, "清空缓存成功", 100).show();
			default:
				break;
			}
		}
};
}